package com.google.code.mochaccino.framework.crypto;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.crypto.cipher.CipherHandlerBlockDoFinal;
import com.google.code.mochaccino.framework.crypto.cipher.CipherHandlerBlockUpdate;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemeAsymmetric;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemePasswordBased;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemeSymmetric;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import org.junit.Test;

public class EncryptionTest_Algo extends EncryptionTest {

	@Test
	public void testDES() throws EncryptionException {
		byte[] key = EncryptionSchemeSymmetric.generateKey( "DES", EncryptionProvider.getBouncyCastleName() );
		EncryptionSchemeSymmetric encrypter = new EncryptionSchemeSymmetric( key, "DES", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		performEncryptionTest( encrypter );

		encrypter = new EncryptionSchemeSymmetric( key, "DES", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		performEncryptionTest( encrypter );
	}

	@Test
	public void testDESede() throws EncryptionException {
		byte[] key = EncryptionSchemeSymmetric.generateKey( "DESede", EncryptionProvider.getBouncyCastleName() );
		EncryptionSchemeSymmetric encrypter = new EncryptionSchemeSymmetric( key, "DESede", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		performEncryptionTest( encrypter );
	}

	@Test
	public void testPBE_TwoFish() throws EncryptionException {
		int keySize = 256;
		int iterationCount = 3;
		String algo = "PBEWithSHAAndTwofish-CBC";
		String passphrase = "my-passphrase";
		String salt = "salt";
		EncryptionSchemePasswordBased encrypter = new EncryptionSchemePasswordBased( passphrase, keySize, salt, iterationCount, algo, EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		performEncryptionTest( encrypter );
	}

	@Test
	public void testRSA() throws EncryptionException, NoSuchAlgorithmException, NoSuchProviderException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "RSA", EncryptionProvider.getBouncyCastleName() );
		keyPairGenerator.initialize( 2048 );
		KeyPair kp = keyPairGenerator.generateKeyPair();
		EncryptionSchemeAsymmetric encrypter = new EncryptionSchemeAsymmetric( "RSA", EncryptionProvider.getBouncyCastleName(), kp.getPublic(), kp.getPrivate(), new CipherHandlerBlockDoFinal( 2048 / 8 - 11 ) );
		performEncryptionTest( encrypter );
	}
}
